<?php

namespace App\Http\Middleware;

use App\Http\Common\MyUtils;
use App\Models\AreaApp;
use App\Models\AreaAppSoft;
use App\Models\Developer\Developer;
use App\Models\Developer\DeveloperPackage;
use App\Models\Developer\DeveloperPackageApp;
use Closure;
use Illuminate\Http\Request;

class DeveloperOpenMiddleware
{
    /**
     * Handle an incoming request.
     * @param  Request  $request
     * @param Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // 获取access_token
        if (!$request->access_token) {
            return response(MyUtils::dealFD('',20001,['retmsg'=>'请传递access_token']));
        }
        if (!$request->app_only_id) {
            return response(MyUtils::dealFD('',20000,['retmsg'=>'请传递app_only_id']));
        }
        $developerPackageApp = DeveloperPackageApp::where(['app_unique_id'=>$request->app_only_id])
            ->first();
        if (!$developerPackageApp){
            return response(MyUtils::dealFD('',20007,['retmsg'=>'该bucket_id/bucket_secret套件内的应用已被删除']));
        }
        if (!$developerPackageApp->enable) {
            return response(MyUtils::dealFD('',20007,['retmsg'=>'该bucket_id/bucket_secret套件内的应用已被禁用']));
        }
        // 得到当前小区是否购买
        $areaAppSoft = AreaAppSoft::where(['app_id'=>$developerPackageApp->id])->first();
        if (!$areaAppSoft){
            return response(MyUtils::dealFD('',20007,['retmsg'=>'该小区没有购买该应用，请在应用商城购买应用']));
        }
        // 验证access_token
        $res = MyUtils::getInstance()->parseToken($request->access_token);
        if ($res['code']<0) {// access_token 解析失败
            return response(MyUtils::dealFD('',20002,['retmsg'=>'access_token已经过期，请重新获取access_token']));
        }
        $data = $res['data'];
        $developerPackage = DeveloperPackage::where([
            "unique_id"=>$data['bucket_id']
            ,"unique_key"=>$data['bucket_secret']
        ])->first();
        if (!$developerPackage) {// 没有查到该access_token解析的值
            return response(MyUtils::dealFD('',20003,['retmsg'=>'该bucket_id/bucket_secret套件已被删除，请重新获取']));
        }
        // 得到该套件是否有权限
        if ($developerPackage->enable!==1) {
            return response(MyUtils::dealFD('',20005,['retmsg'=>'该bucket_id/bucket_secret套件未启用']));
        }
        $developer = Developer::find($developerPackage->developer_id);
        if (!$developer){
            return response(MyUtils::dealFD('',20009,['retmsg'=>'该bucket_id/bucket_secret套件所属的开发者被删除']));
        }
        // 得到该开发者是否有权限
        if ($developer->enable!==1) {
            return response(MyUtils::dealFD('',20006,['retmsg'=>'该bucket_id/bucket_secret套件所属的开发者被禁用']));
        }
        // 开发者有权限就直接在这里
        MyUtils::setRequest($request,'onlyKey',$res['data']);// 唯一码
        MyUtils::setRequest($request,'developer',$developer);// 开发者
        MyUtils::setRequest($request,'developerPackage',$developerPackage);// 开发者套件
        MyUtils::setRequest($request,'developerPackageApp',$developerPackageApp);// 开发者套件应用
        return $next($request);
    }
}
